package com.superf.taobao.service;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        //获取前端页面输入的用户名和密码
        String username = req.getParameter("username");
        String password = req.getParameter("password");


        if(username.equals("wangcai") && password.equals("111")){
            //服务器端创建一个session，并把用户名保存进去作为一个登录过的标识
            HttpSession session = req.getSession();//创建一个session或者存在的话直接获取
            session.setAttribute("user", username);
            resp.sendRedirect(req.getContextPath()+"/showAllBooks");
        }else{
            resp.sendRedirect(req.getContextPath()+"/login.html");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
